package com.huohuo.concurrent.zk;

import com.huohuo.concurrent.simple.OrderNumberGenerator;

import java.util.Collections;
import java.util.concurrent.locks.Lock;

public class OrderZkService implements Runnable{


    private OrderNumberGenerator orderNumberGeneratorLock = new OrderNumberGenerator();

    private LockInterface lock = new ZkDistrbuteLock();



    /**
     * 使用多线程模拟很多的请求
     */
    @Override
    public void run() {
        try {
            lock.getLock();
            String number = orderNumberGeneratorLock.generatorNumber();
            System.out.println("线程名称："+Thread.currentThread().getName()+"订单号："+number);

        }catch (Exception e){
            e.printStackTrace();;
        }finally {
            lock.unLock();
        }


    }

    /**
     * 方法的测试
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        for (int i=0;i<50;i++){
            new Thread( new OrderZkService()).start();
        }
        /*Thread.sleep( 1000 );
        Collections.sort( result );
        for (String str:result){
            System.out.println("订单号："+str);
        }*/
    }




}
